package com.ruoyi.project.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.project.domain.ProjectRecoveryDetail;
import com.ruoyi.project.domain.RecoveryNode;
import com.ruoyi.project.service.IProjectRecoveryDetailService;
import com.ruoyi.project.service.impl.ProjectRecoveryDetailServiceImpl;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.project.domain.ProjectRecovery;
import com.ruoyi.project.service.IProjectRecoveryService;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 工程复盘目录Controller
 *
 * @author liboyu
 * @date 2024-05-29
 */
@RestController
@RequestMapping("/project/recovery")
public class ProjectRecoveryController extends BaseController
{
    @Autowired
    private IProjectRecoveryService projectRecoveryService;

    @Autowired
    private IProjectRecoveryDetailService projectRecoveryDetailService;

    /**
     * 查询工程复盘目录列表
     */
    @PreAuthorize("@ss.hasPermi('project:recovery:list')")
    @GetMapping("/list")
    public AjaxResult list(ProjectRecovery projectRecovery)
    {
        List<ProjectRecovery> list = projectRecoveryService.selectProjectRecoveryList(projectRecovery);
        return success(list);
    }

    /**
     * 导出工程复盘目录列表
     */
    @PreAuthorize("@ss.hasPermi('project:recovery:export')")
    @Log(title = "工程复盘目录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProjectRecovery projectRecovery)
    {
        List<ProjectRecovery> list = projectRecoveryService.selectProjectRecoveryList(projectRecovery);
        ExcelUtil<ProjectRecovery> util = new ExcelUtil<ProjectRecovery>(ProjectRecovery.class);
        util.exportExcel(response, list, "工程复盘目录数据");
    }

    /**
     * 获取工程复盘目录详细信息
     */
    @PreAuthorize("@ss.hasPermi('project:recovery:query')")
    @GetMapping(value = "/{recoveryId}")
    public AjaxResult getInfo(@PathVariable("recoveryId") Long recoveryId)
    {
        return success(projectRecoveryService.selectProjectRecoveryByRecoveryId(recoveryId));
    }

    /**
     * 新增工程复盘目录
     */
    @PreAuthorize("@ss.hasPermi('project:recovery:add')")
    @Log(title = "工程复盘目录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProjectRecovery projectRecovery)
    {
        //唯一验证
        Long id =projectRecoveryService.checkNameUnique(projectRecovery);
        if (id != null){
            return error("本节点现已有同名节点,不能添加");
        }
        return toAjax(projectRecoveryService.insertProjectRecovery(projectRecovery));
    }

    /**
     * 修改工程复盘目录
     */
    @PreAuthorize("@ss.hasPermi('project:recovery:edit')")
    @Log(title = "工程复盘目录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProjectRecovery projectRecovery)
    {
        //唯一验证
        Long id =projectRecoveryService.checkNameUnique(projectRecovery);
        if (id != null){
            return error("本节点现已有同名节点,不能修改");
        }
        return toAjax(projectRecoveryService.updateProjectRecovery(projectRecovery));
    }

    /**
     * 删除工程复盘目录
     */
    @PreAuthorize("@ss.hasPermi('project:recovery:remove')")
    @Log(title = "工程复盘目录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{recoveryIds}")
    public AjaxResult remove(@PathVariable Long[] recoveryIds)
    {
        //查询是否有子节点
        Long id =projectRecoveryService.getIdByPid(recoveryIds[0]);
        if (id != null){
            return error("请先删除子节点,再删除本节点.");
        }

        //查询是否外键冲突
        Long id2 = projectRecoveryDetailService.getFkId(recoveryIds[0]);
        if (id2 != null){
            return error("主键为"+id2+"的工程服盘详情使用了本条记录,不能删除.请先删除对应数据");
        }

        return toAjax(projectRecoveryService.deleteProjectRecoveryByRecoveryIds(recoveryIds));
    }

    /**
     * 获取树表
     */
    @PreAuthorize("@ss.hasPermi('project:recovery:query')")
    @GetMapping("/getTree/{projectId}")
    public AjaxResult getTree(@PathVariable Long projectId)
    {
        //获得根节点
        RecoveryNode root=projectRecoveryService.getTree(projectId);
        return success(root);
    }
}
